from PySide import QtCore
from logic.cards import Deck
from logic.fst import StateMachine, fillStateMachine
import notifications
from puremvc.patterns.proxy import Proxy


__author__ = 'wadim'

class PlayGameModelProxy(Proxy):
    NAME = "PlayGameModelProxy"

    def __init__(self):
        super(PlayGameModelProxy,self).__init__(PlayGameModelProxy.NAME,[])
        #: @type: StateMachine
        self.state_machine = None
        self.deck = Deck()
        self.displayed_p_cards = []
        self.displayed_cards = []


    def setAction(self,p_action):
        if self.state_machine.currentState:
            self.state_machine.currentState.action = p_action

    def next(self):
        old_state = self.state_machine.currentState.name
        state_name = self.state_machine.next()
        if old_state == "player_turn_state":
            self.updatePlayerMoney()
            self.updateBankMoney()
        elif  old_state == "cp_turn_state":
            self.updateCPUMoney()
            self.updateBankMoney()

        if state_name == "player_turn_state":
            self.sendNotification(notifications.PLAYER_TURN,self.state_machine.currentState)
        if state_name == "cp_turn_state":
            self.sendNotification(notifications.CPU_TURN, self.state_machine.currentState)
        if state_name == "end_turn_state":
            self.sendNotification(notifications.ENDTURN, self.state_machine.currentState)
        if state_name == "finish_state":
            self.sendNotification(notifications.FINISH, self.state_machine.currentState)

    def getPlayerCombination(self):
        return self.state_machine.getPlayerCombination()

    def reset(self):
        self.state_machine = StateMachine()
        fillStateMachine(self.state_machine)
        self.deck.reset()
        self.state_machine.p_cards = []
        self.sendNotification(notifications.STARTGAME)
        self.next()


    def addPlayerCard(self):
        self.state_machine.p_cards.append(self.deck.getCard())

    def addCard(self):
        self.state_machine.cards.append(self.deck.getCard())

    def updatePlayerMoney(self):
        self.sendNotification(notifications.CHANGEMONEY, self.state_machine.p_money)
    def updateCPUMoney(self):
        self.sendNotification(notifications.CHANGECPUMONEY, self.state_machine.c_money)
    def updateBankMoney(self):
        self.sendNotification(notifications.CHANGEBANKMONEY, self.state_machine.bank_money)

    def getCurrentState(self):
        return self.state_machine.currentState
    def getPCheck(self):
        return self.state_machine.p_check
    def getCPUCheck(self):
        return self.state_machine.cpu_check
    def setPCheck(self, p_val):
        self.state_machine.p_check = p_val
    def setCPUCheck(self, p_val):
        self.state_machine.cpu_check = p_val
    def getMin(self):
        return self.state_machine.min
    def getPPut(self):
        return self.state_machine.dropped_money

    def getRawPCards(self):
        return self.state_machine.p_cards
    def getRawCards(self):
        return self.state_machine.cards

    def getPCards(self):
        cards = []
        for i in self.state_machine.p_cards:
            if i not in self.displayed_p_cards:
                self.displayed_p_cards.append(i)
                cards.append(i)
        return cards
    def getCCards(self):
        return self.state_machine.c_cards
    def getCards(self):
        cards = []
        for i in self.state_machine.cards:
            if i not in self.displayed_cards:
                self.displayed_cards.append(i)
                cards.append(i)
        return cards
    def getPMoney(self):
        return self.state_machine.p_money
    def getCMoney(self):
        return self.state_machine.c_money
    def getPutPMoney(self):
        return self.state_machine.p_put_money
    def getBankMoney(self):
        return self.state_machine.bank_money

    def incrasePutPMoney(self,p_value):
        self.state_machine.dropped_money+=p_value
        if self.state_machine.dropped_money < self.state_machine.min:
            self.state_machine.dropped_money = self.state_machine.min

        if self.state_machine.dropped_money > self.state_machine.max:
            self.state_machine.dropped_money = self.state_machine.max

        self.sendNotification(notifications.CHANGEBANKMONEY, self.state_machine.dropped_money)

